package com.ft.admix.bd.adapter;

import android.app.Activity;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.KsScene;
import com.kwad.sdk.api.KsSplashScreenAd;
import com.ft.admix.ad.MixCustomSplashAdapter;
import com.ft.admix.common.MixConstant;
import com.ft.admix.config.AdConfig;
import com.ft.admix.utils.ThreadUtils;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class SplashAdapterLoader extends MixCustomSplashAdapter {
    private KsSplashScreenAd mSplashAd;
    private View splashView;

    @Override
    protected void loadNetworkAd(Activity context, AdConfig config) {
        if (!KsAdSDK.haseInit()||KsAdSDK.getLoadManager() == null) {
            callLoadFailed(-1, "快手SDK未初始化");
            return;
        }

        KsScene ksScene = new KsScene.Builder(Long.parseLong(config.getAdId())).build();
//                requestParams.setPosId(Long.parseLong(config.getAdId()));
        KsAdSDK.getLoadManager().loadSplashScreenAd(ksScene, new KsLoadManager.SplashScreenAdListener() {
            @Override
            public void onError(int i, String s) {
                callLoadFailed(i, s);
            }

            @Override
            public void onRequestResult(int i) {

            }

            @Override
            public void onSplashScreenAdLoad(@Nullable @androidx.annotation.Nullable KsSplashScreenAd ksSplashScreenAd) {
                if (ksSplashScreenAd != null) {
                    mSplashAd = ksSplashScreenAd;
                    splashView = mSplashAd.getView(context, new KsSplashScreenAd.SplashScreenAdInteractionListener() {
                        @Override
                        public void onAdClicked() {
                            callSplashAdClicked();
                        }

                        @Override
                        public void onAdShowError(int i, String s) {
                            callSplashAdShowError(i, s);
                        }

                        @Override
                        public void onAdShowEnd() {
                            callSplashAdDismiss();
                        }

                        @Override
                        public void onAdShowStart() {
                            callSplashAdShow();
                        }

                        @Override
                        public void onSkippedAd() {
                            callSplashAdSkip();
                        }

                        @Override
                        public void onDownloadTipsDialogShow() {

                        }

                        @Override
                        public void onDownloadTipsDialogDismiss() {

                        }

                        @Override
                        public void onDownloadTipsDialogCancel() {

                        }
                    });
                    callLoadSuccess(mSplashAd.getECPM());
                }
            }
        });

    }

    @Override
    public void show(ViewGroup container) {
        ThreadUtils.runOnUIThreadByThreadPool(new Runnable() {
            @Override
            public void run() {
                if (mSplashAd != null && container != null) {
                    if (splashView != null) {
                        ViewParent parent = splashView.getParent();
                        if (parent instanceof ViewGroup) {
                            ((ViewGroup) parent).removeView(splashView);
                        }
                        splashView.setLayoutParams(new
                                ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                ViewGroup.LayoutParams.MATCH_PARENT));
                        container.removeAllViews();
                        container.addView(splashView);
                    }
                }
            }
        });
    }

    @Override
    public void destroy() {
        if (splashView != null) {
            ViewParent parent = splashView.getParent();
            if (parent instanceof ViewGroup) {
                ((ViewGroup) parent).removeView(splashView);
            }
        }
    }

    @Override
    public MixConstant.AdReadyStatus isReadyCondition() {
        /**
         * 在子线程中进行广告是否可用的判断
         */
        Future<MixConstant.AdReadyStatus> future = ThreadUtils.runOnThreadPool(new Callable<MixConstant.AdReadyStatus>() {
            @Override
            public MixConstant.AdReadyStatus call() throws Exception {
                if (mSplashAd != null && mSplashAd.isAdEnable()) {
                    return MixConstant.AdReadyStatus.AD_IS_READY;
                } else {
                    return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
                }
            }
        });
        try {
            MixConstant.AdReadyStatus result = future.get(500, TimeUnit.MILLISECONDS);
            if (result != null) {
                return result;
            } else {
                return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
    }
} 